<?php
class Doctrine_Tools extends Controller {
	public function __construct() {
		parent::Controller();
		//parent::MY_SecuredController();

		//$this->requireRole('Owner');

		$this->load->helper(array('form','url'));
		//$this->load->library('nusoapwrapper');
	}

	function index() {
		echo '<a href="doctrine_tools/create_models">Create Models</a><br />';
		echo '<a href="doctrine_tools/create_tables">Create Tables</a><br />';
	}

	function create_models() {
		echo '<form action="" method="POST">
		<input type="submit" name="action" value="Create s"><br /><br />';

		if ($this->input->post('action')) {
			try {
				$options = array(
				//'packagesPrefix'  =>  'Plugin',
				//'baseClassName'   =>  'MyDoctrineRecord',
				  'generateBaseClasses' =>	true,
				  'baseClassesDirectory' => '',
				  'suffix'          =>  '.php'
				  );
				  Doctrine::generatesFromYaml(APPPATH.'fixture/schema.yml', APPPATH.'/models', $options);
			}
			catch (Exception $e) {
				echo '<pre>';
				echo $e;
				echo '</pre>';
			}
			echo "Done!";
		}
	}

	function create_leonardo_models() {
		echo '<form action="" method="POST">
        <input type="submit" name="action" value="Create s"><br /><br />';

		if ($this->input->post('action')) {
			try {
				$options = array(
				//'packagesPrefix'  =>  'Plugin',
				//'baseClassName'   =>  'MyDoctrineRecord',
                  'generateBaseClasses' =>  true,
                  'baseClassesDirectory' => '',
                  'suffix'          =>  '.php'
                  );
                  Doctrine::generatesFromYaml(APPPATH.'fixture/db.yml', APPPATH.'/models', $options);
			}
			catch (Exception $e) {
				echo '<pre>';
				echo $e;
				echo '</pre>';
			}
			echo "Done!";
		}
	}

	public function save_data() {
		echo '<form action="" method="POST">
        <input type="submit" name="action" value="Dump Data"><br /><br />';

		if ($this->input->post('action')) {
			try {
				Doctrine::debug(true);
				Doctrine::dumpData(APPPATH.'fixture/DataFromDb.yml');
			}
			catch (Exception $e) {
				echo '<pre>';
				echo $e;
				echo '</pre>';
			}
			echo "Done!";
		}
	}

	public function dump_db() {
		echo '<form action="" method="POST">
        <input type="submit" name="action" value="Dump Data"><br /><br />';

		if ($this->input->post('action')) {
			try {
				Doctrine::debug(true);
				//Doctrine::generateYamlFromDb(APPPATH.'fixture/db.yml');
				Doctrine::generateYamlFromModels(APPPATH.'fixture/SchemaFromModels.yml', APPPATH.'/models');
			}
			catch (Exception $e) {
				echo '<pre>';
				echo $e;
				echo '</pre>';
			}
			echo "Done!";
		}
	}


	public function load_data() {
		echo '<form action="" method="POST">
        <input type="submit" name="action" value="Load Data"><br /><br />';

		if ($this->input->post('action')) {
			try {
				//Doctrine_Manager::connection()->execute('SET FOREIGN_KEY_CHECKS = 0');
				Doctrine::loadData(APPPATH.'fixture/data.yml');
				//Doctrine_Manager::connection()->execute('SET FOREIGN_KEY_CHECKS = 1');
			}
			catch (Exception $e) {
				echo '<pre>';
				echo $e;
				echo '</pre>';
			}
			echo "Done!";
		}
	}

	function create_tables() {
		Doctrine::debug(true);

		echo 'Reminder: Make sure the tables do not exist already.<br />
		<form action="" method="POST">
		<input type="submit" name="action" value="Create Tables"><br /><br />';

		if ($this->input->post('action')) {
			//$sql = Doctrine::generateSqlFromModels();
			//echo '<pre>'.$sql.'</pre>';

			try {
				//Doctrine::dropDatabases();
				//Doctrine::createDatabases();
				//Doctrine::createTablesFromModels();
				Doctrine::createTablesFromArray(array(
					'ClubAdminModel',
					'ClubAuthorityModel',
					'ClubContactModel',
					'ClubModel',
					'ClubPilotModel',
					'CompetitionModel',
					'CompetitionPilotModel',
					'CompetitionTypeModel',
					'GliderModel',
					'LicenseModel',
					'PersonModel',
					'PersonRoleModel',
					'PilotGliderModel',
					'PilotLicenseModel',
					'PilotModel',
					'RoleModel',
					'TaskModel',
					'TaskResultModel'
					));
			}
			catch (Exception $e) {
				echo '<pre>';
				echo $e;
				echo '</pre>';
			}
			echo "Done!";
		}
	}

	function drop_tables() {
		//Doctrine::dropDatabases();
		Doctrine_Manager::connection()->execute('SET FOREIGN_KEY_CHECKS = 0');
		$query = 'DROP TABLE `portal_club`, `portal_club_admin`, `portal_club_authority`, `portal_club_contact`, `portal_club_pilot`, `portal_competition`, `portal_competition_task`, `portal_competition_type`, `portal_glider`, `portal_license`, `portal_person`, `portal_person_role`, `portal_pilot`, `portal_pilot_competition`, `portal_pilot_glider`, `portal_pilot_license`, `portal_role`, `portal_task_result`;';

		Doctrine_Manager::connection()->execute($query);
		Doctrine_Manager::connection()->execute('SET FOREIGN_KEY_CHECKS = 1');
	}

	function create_tables_sql() {
		echo '<pre>';
		var_dump(Doctrine::generateSqlFromArray(array(
                    'ClubAdminModel', 'ClubAuthorityModel', 'ClubContactModel', 'ClubModel', 'CompetitionModel', 'CompetitionPilotModel',
                    'CompetitionTypeModel', 'GliderModel', 'LicenseModel', 'PersonModel', 'PersonRoleModel', 'PilotGliderModel',
                    'PilotLicenseModel', 'PilotModel', 'RoleModel', 'TaskModel', 'TaskResultModel', 'ClubPilot'
                    )));
                    echo '</pre>';
	}

	function test_sql() {
		//$result = ClubModel::getLeonardoClub(1);
		$nacId = 1;

		Doctrine::debug(true);

		$q = Doctrine_Query::create()
		->select('n.clubname')
		->from('LeonardoNacClubs n')
		->where('n.nac_id = 54')
		->andWhere('n.clubid = ?', 2);

		echo 'sql: '.$q->getSqlQuery();

		$results = $q->execute(null, Doctrine::HYDRATE_ARRAY);
		echo '<pre> nacClub: '; print_r($results); echo '</pre>';

		//$nacClub = Doctrine::getTable('LeonardoNacClubs')->findByNacIdAndClubId(54, 2);
		echo '<pre> nacClub: '; echo $results[0]['clubname'];  echo '</pre>';

		//$result = Doctrine::getTable("ClubModel")->findOneByName($nacClub->name);

		//echo '<pre>'; echo print_r($result->clubName); echo '</pre>';
	}

	function test_soap() {
		$this->load->library("Nusoap_lib");

		$wsdlURL = "http://civlrankings.fai.org/FL.asmx?wsdl";
		$parameters['parameters']['name'] = 'Koch';

		//$result = $this->nusoap_client->call("FindPilot", $parameters);

		$this->load->library('Nusoap_lib');
		$this->soapclient = new nusoap_client($wsdlURL, "wsdl");
		//print_r($this->soapclient);

		if(!$this->soapclient->fault) {
			if(!$this->soapclient->getError()) {
				$result = $this->soapclient->call('FindPilot', $parameters);

				$i = 0;

				if ($result['FindPilotResult']['Person']['Name']) {
					$tmp = $result['FindPilotResult']['Person'];
					unset($result['FindPilotResult']['Person']);
					
					$result['FindPilotResult']['Person'][0] = $tmp;
				}
			}
		}
	}
}
